<template>
  <view class="box">
    <Header title="采购单核验" :autoBack="false" @leftClick="back"></Header>
    <view :style="{'margin-top':topHeight.top+'px'}">
      <Tabs :list="list" :scrollable="false" @change-tab="changeVerifyType"></Tabs>
      <!--  订单列表组件  -->
    </view>
    <scroll-view scroll-y="true" refresher-enabled="true" :refresher-triggered="triggered"
                 :lower-threshold="50" @scrolltolower="nextPage" :style="{height: `calc(100vh - 44px - ${topHeight.top}px)`}"
                 :refresher-threshold="80" refresher-background="rgb(244, 244, 244)" @refresherpulling="onPulling"
                 @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort">
      <view v-if="dataList.length > 0">
        <view class="option-box" v-for="(item,index) in dataList" :key="index" @click="handlePurchaseCheckDone(item)">
          <order-item :title="item.providerOrgName" :form="item">
            <template #button>
              <button v-if="item.verificationFlag != 1" class="blue-button" @click.stop="handlePurchaseCheck(item)">开始核验</button>
            </template>
          </order-item>
        </view>
        <uni-load-more :status="loadingType" :content-text="contentText"/>
      </view>
      <view v-if="emptyFlag" class="empty-box">
        <uv-empty mode="data" icon="/static/image/no-data.png" width="200rpx" height="260rpx" text="&nbsp;"></uv-empty>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import {ref, reactive, getCurrentInstance} from "vue";

import orderItem from '../components/orderList/orderItem.vue'
const {proxy} = getCurrentInstance();
import api from "@/api/school/order/order";

const list = ref([
  {name: '待核验', value: '0'},
  {name: '已核验', value: '1'},
])
let dataList = ref([]);
let queryForm = reactive({         // 查询条件
  pageSize: 10,
  pageNum: 1,
  orderStatus: 100,
  verificationFlag: '0',
});
let total = ref(0);
let freshing = ref(false);
let triggered = ref(false);
let emptyFlag = ref(false);
let loadingType = ref('noMore');
let contentText = reactive({
  contentdown: "上拉显示更多",
  contentrefresh: "正在加载...",
  contentnomore: "没有更多数据了"
});
// tab切换
let changeVerifyType = (e) => {
  queryForm.verificationFlag = e.value;
  getPurchaseList()
}
// 获取数据
let getPurchaseList = async (e) => {
  loadingType.value = 'loading';
  dataList.value=[];
  let res = await proxy.http.asyncPost(api.schoolOrderList, queryForm);
  if (res.code === 0) {
    total.value = res.data.total;
    dataList.value = queryForm.pageNum === 1 ? res.data.records : dataList.value.concat(res.data.records);
    console.log(dataList.value);
    if (dataList.value.length < total.value) {
      loadingType.value = 'more';
    } else {
      loadingType.value = 'noMore'; //没有数据时显示‘没有更多’
    }
    if (dataList.value.length > 0) {
      emptyFlag.value = false
    } else {
      emptyFlag.value = true
    }
  } else {
    uni.showToast({title: res.msg, icon: 'none'});
  }
}
// 开始核验
  let handlePurchaseCheck = (item) =>{
  uni.navigateTo({
    url: '/pages/customer/purchaseOrderVerification/purchaseOrderDetail?order=' + encodeURIComponent(JSON.stringify(item)),
  })
}
// 已核验详情
let handlePurchaseCheckDone = (item)=> {
  uni.navigateTo({
    url: '/pages/customer/purchaseOrderVerification/purchaseOrderDetail?order=' +encodeURIComponent(JSON.stringify(item))+`&type=view`,
  })
}
let nextPage = (e) => {
  if (queryForm.pageNum * queryForm.pageSize < total.value) {
    queryForm.pageNum++;
    getPurchaseList()
  }
}
// 下拉刷新
let onPulling = (e) => {
  freshing.value = false;
  setTimeout(() => {
    triggered.value = true;
  }, 1000)
  console.log("下拉刷新了")
}
// 加载
let onRefresh = (e) => {
  if (freshing.value) return;
  triggered.value = 'restore';
  setTimeout(() => {
    freshing.value = false;
    triggered.value = false;
  }, 1000)
  queryForm.pageNum = 1
  getPurchaseList();
}
let onRestore = (e) => {
  triggered.value = 'restore';
}
let onAbort = (e) => {
  console.log("onAbort");
}
// 返回首页
let back=()=>{
  uni.switchTab({
    url: '/pages/application/applicationCenter'
  });
}
getPurchaseList();
</script>

<style scoped lang="scss">
.box {
  height: 100vh;
  overflow-y: auto;
}

::v-deep .uv-tabs__wrapper__scroll-view .uni-scroll-view::-webkit-scrollbar {
  display: none !important;
}

</style>
